/**
 * @Title: CircleRatioView.java
 * @Package com.jzg.jzgoto.phone.tools.view.circleratio
 * @Description: TODO
 * Copyright: Copyright (c) 2011 
 * Company:北京精真估信息技术有限公司
 * 
 * @author Comsys-wangyd
 * @date 2015-12-8 下午1:51:06
 * @version V1.0
 */

package com.jzg.jcptadmin.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: CircleRatioView
 * @Description: 用于新车使用成本中图片上显示燃油、保养、税费、利息、保险的使用百分比
 * @author Comsys-wangyd
 * @date 2015-12-8 下午1:51:06
 * 
 */

public class CircleRatioView extends View
{
	/**
	 * 
	 * @ClassName: CircleRatioListener
	 * @Description: 监听需要绘制的数据
	 * @author Comsys-wangyd
	 * @date 2015-12-8 下午2:20:09
	 * 
	 */
	interface CircleRatioListener
	{
		public List<Double> setData();
	}

	private CircleRatioListener circleRatioListener;

	private static final int ONE = 1;
	private static final int TWO = 2;
	private static final int THREE = 3;
	private static final int FOUR = 4;
	private static final int FIVE = 5;



	// 默认圆的最小半径
	private int defaultMinRadius;

	// 默认圆的最大半径
	private int defaultMaxRadius;

	// 默认字体颜色
	private int defaultFontColor = Color.WHITE;
	// 默认字体颜色--描述
	private int defaultFontColor1 = Color.BLACK;

	// 默认字体大小
	private int defaultFontSize = 14;

	// 当前绘制圆的x
	private float curX;

	// 当前绘制圆的y
	private float curY;

	// 当前显示的文本内容
	private String curText = "";

	// 当前显示的文本数据值
	private double curData = 0.0;

	// 当前圆颜色值
	private int curColor;

	// 当前绘制圆的半径
	private int curRadius;

	private Context context;

	// 空心圆画笔
	private Paint borderCirclePaint;

	// 空心圆心x
	private float borderCircleCenterX;

	// 空心圆心y
	private float borderCircleCenterY;

	// 空心圆半径
	private int borderRadius;

	// 空心圆直径
	private int borderWidth = 0;

	// 实心圆画笔
	private Paint solidCirclePaint;

	// 图片背景画笔
	private Paint picPaint;

	// 布局时的开始角度
	private double startAngle = 0;

	// 字体画笔
	private Paint textPaint;
	// 备注字体画笔
	private Paint textPaint1;

	// 实心圆半径的增长值
	private int index = 0;
	// 绘制的5个圆相关的颜色信息
	private  int[] colors = {};
	List<Double> datas = new ArrayList<Double>();
	
	//最大值
	private double MaxData;

	/**
	 * 设置5个圆的数据和最大值
	 * @param
	 */
	public void setDatas(Map<Integer,Double> dataMap,double max){

		if(dataMap!=null){

		int[] color = new int[dataMap.size()];

		List<Integer> mapList = new ArrayList<Integer>();

		Iterator<Integer> it = dataMap.keySet().iterator();
		while (it.hasNext()) {

			mapList.add(it.next());
		}
		List<Double> dataList = new ArrayList<Double>();

		for(int i=0;i<mapList.size();i++){
			color[i] = mapList.get(i);
			dataList.add(dataMap.get(mapList.get(i)));
		}
		this.datas = dataList;

		setColors(color);
		}
//		List<Double> dataList = new ArrayList<Double>();
//		if(dataMap!=null){
//			Iterator<Double> it = dataMap.keySet().iterator();
//			while (it.hasNext()) {
//				dataList.add(it.next());
//			}
//
//			this.datas = dataList;
//			int[] color = new int[dataList.size()];
//			for(int i=0;i<dataList.size();i++){
//				color[i] = dataMap.get(dataList.get(i));
//			}
//			setColors(color);
//		}

		//排序获取最大值
//		Collections.sort(dataList);
//		MaxData = dataList.get(dataList.size()-1);
		MaxData = max;

	}
	/**
	 * 设置5个圆的大小
	 * @param colors
	 */
	public void setColors(int[] colors){
		this.colors = colors;
	}
	public CircleRatioView(Context context)
	{
		this(context, null);
	}

	public CircleRatioView(Context context, AttributeSet attrs)
	{
		this(context, attrs, 0);
	}

	public CircleRatioView(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		this.context = context;
		init();
	}

	public void init()
	{
		this.borderCirclePaint = new Paint();
		this.solidCirclePaint = new Paint();
		this.textPaint = new Paint();
		this.textPaint.setColor(defaultFontColor);
		this.textPaint.setTextSize(dip2px(context, defaultFontSize));
		this.textPaint.setLinearText(true);
		this.textPaint1 = new Paint();
		this.textPaint1.setColor(defaultFontColor1);
		this.textPaint1.setTextSize(dip2px(context, defaultFontSize));
		this.textPaint1.setLinearText(true);


	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	{
		// 获取实际宽、高
		int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
		int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
		// 根据宽、高 设置圆的大小
		int specSize = Math.min(widthSpecSize, heightSpecSize);
		borderWidth = specSize / 10 * 6;
		borderRadius = borderWidth / 2;

		if (widthSpecMode == MeasureSpec.AT_MOST
				&& heightSpecMode == MeasureSpec.AT_MOST)
		{
			setMeasuredDimension(widthSpecSize, heightSpecSize);
		} else if (widthSpecMode == MeasureSpec.AT_MOST)
		{
			setMeasuredDimension(widthSpecSize, heightSpecSize);
		} else if (heightSpecMode == MeasureSpec.AT_MOST)
		{
			setMeasuredDimension(widthSpecSize, 200);
		}
	}

	@Override
	protected void onDraw(Canvas canvas)
	{
		// List<Double> datas = circleRatioListener.getCircleData();
		canvas.save();

		// 画图片
		Bitmap bitmap = Bitmap.createBitmap(400, 300, Config.ARGB_8888);
//		Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
//				R.drawable.act_splash_bg);
//		canvas.drawColor(Color.WHITE);
		canvas.drawBitmap(bitmap, 0, 0, picPaint);

		// 画最外层的圆环
		drawBorderCircle(canvas);
		curRadius = borderRadius / 4;

		// 画实心圆
		drawSolidCircle(canvas, ONE);
		drawSolidCircle(canvas, TWO);
		drawSolidCircle(canvas, THREE);
		drawSolidCircle(canvas, FOUR);
		drawSolidCircle(canvas, FIVE);

		// 取出数值中的最大值，如果index小于每个实心圆取出的data值，则重绘达到绘制动画的效果
		// 如果data的值小于index，说明当前圆的动画已经绘制，则使用默认半径加data的值绘制实圆
		if (index < MaxData)
		{
			invalidate();// 重构View
		}
		index++;
	}

	private void drawSolidCircle(final Canvas canvas, int position)
	{
		// 根据绘制圆的位置信息分别设置每个圆对应的x，y轴中心点
		switch (position) {
		case ONE:
			curX = borderCircleCenterX;
			curY = borderCircleCenterY - borderRadius;
			break;
		case TWO:
			curX = (float) (borderCircleCenterX + borderRadius
					* Math.cos(Math.toRadians(18)));
			curY = (float) (borderCircleCenterY - borderRadius
					* Math.cos(Math.toRadians(72)));
			break;
		case THREE:
			curX = (float) (borderCircleCenterX + borderRadius
					* Math.cos(Math.toRadians(54)));
			curY = (float) (borderCircleCenterY + borderRadius
					* Math.cos(Math.toRadians(36)));
			break;
		case FOUR:
			curX = (float) (borderCircleCenterX - borderRadius
					* Math.cos(Math.toRadians(54)));
			curY = (float) (borderCircleCenterY + borderRadius
					* Math.cos(Math.toRadians(36)));
			break;
		case FIVE:
			curX = (float) (borderCircleCenterX - borderRadius
					* Math.cos(Math.toRadians(18)));
			curY = (float) (borderCircleCenterY - borderRadius
					* Math.cos(Math.toRadians(72)));
			break;
		}

		// 设置圆的颜色，文本，数据值
		setCurCircleInfo(position);

//		LogUtils.e("position is " + position + "curX is " + curX + ",curY is "
//				+ curY + ",curRadius is " + index);

		solidCirclePaint.setColor(curColor);
		solidCirclePaint.setAntiAlias(true);

		// curRadius 相当于默认半径，data大于等于增长值时，实心圆不断增长，当data小于增长值时停止增长
		if (curData >= index)
		{
			canvas.drawCircle(curX, curY, (float) (curRadius + index),
					solidCirclePaint);
			
			switch (position) {
			case ONE:
				int textMeasuresf = (int) textPaint.measureText("税费")/2;
				int textMeasureHightsf = (int) textPaint.measureText("--") / 2;
				canvas.drawText("税费",(float)( curX- textMeasuresf),
						curY-(curRadius+index+textMeasureHightsf), textPaint1);
				break;
			case TWO:
				int textMeasureHight = (int) textPaint.measureText("--") / 2;
				canvas.drawText("燃油", curX+ curRadius +index,
						curY+textMeasureHight, textPaint1);
				break;
			case THREE:
				int textMeasureHightbx = (int) textPaint.measureText("--") / 2;
				canvas.drawText("保险", curX+ curRadius+ index,
						curY+textMeasureHightbx, textPaint1);
				break;
			case FOUR:
				int textMeasureby = (int) textPaint.measureText("保养");
				int textMeasureHightby = (int) textPaint.measureText("--")/2;
				canvas.drawText("保养", (float)(curX-(curRadius + index+textMeasureby)),
						(float)(curY +textMeasureHightby), textPaint1);
				break;
			case FIVE:
				
				int textMeasure = (int) textPaint.measureText("折旧");
				int textMeasureHightzj = (int) textPaint.measureText("--") / 2;
				canvas.drawText("折旧", curX-(curRadius +index+textMeasure),
						(float)(curY+textMeasureHightzj), textPaint1);
				break;
			}
			
			
		} else
		{
			canvas.drawCircle(curX, curY, (float) (curRadius + curData),
					solidCirclePaint);
			switch (position) {
			case ONE:
//				int textMeasuresf = (int) textPaint.measureText("税费");
//				int textMeasureHightsf = (int) textPaint.measureText("--") / 2;
//				canvas.drawText("税费",(float)( curX- (curRadius+curData+textMeasuresf)),
//						curY+textMeasureHightsf, textPaint1);
				
				int textMeasuresf = (int) textPaint.measureText("税费")/2;
				int textMeasureHightsf = (int) textPaint.measureText("--") / 2;
				canvas.drawText("税费",(float)( curX- textMeasuresf),
						(float)(curY-(curRadius+curData+textMeasureHightsf)), textPaint1);
				break;
			case TWO:
				int textMeasureHight = (int) textPaint.measureText("--") / 2;
				canvas.drawText("燃油", (float)(curX+ curRadius + curData),
						curY+textMeasureHight, textPaint1);
				break;
			case THREE:
				int textMeasureHightbx = (int) textPaint.measureText("--") / 2;
				canvas.drawText("保险", (float)(curX+ curRadius + curData),
						curY+textMeasureHightbx, textPaint1);
				break;
			case FOUR:
				int textMeasureby = (int) textPaint.measureText("保养");
				int textMeasureHightby = (int) textPaint.measureText("--")/2;
				canvas.drawText("保养", (float)(curX-(curRadius + curData+textMeasureby)),
						(float)(curY +textMeasureHightby), textPaint1);
				break;
			case FIVE:
				int textMeasure = (int) textPaint.measureText("折旧");
				int textMeasureHightzj = (int) textPaint.measureText("--") / 2;
				canvas.drawText("折旧", (float)(curX-(curRadius +curData+textMeasure)),
						(float)(curY+textMeasureHightzj), textPaint1);
				
				break;
			}
			
		}

		// 如果当前绘制圆的颜色是透明的，说明其传递值为null，则不对text的文本进行绘制
		if (curColor != Color.TRANSPARENT)
		{
			int textMeasure = (int) textPaint.measureText(curText) / 2;
			int textMeasureHight = (int) textPaint.measureText("--") / 2;
			canvas.drawText(curText, curX-textMeasure,
					curY+textMeasureHight, textPaint);
		}
		
		
		
		
		
		
		
	}

	/**
	 * setCurCircleRadians
	 * 
	 * @Title: setCurCircleRadians
	 * @Description: 设置当前实心圆对应的数据值、文本、颜色
	 * @param @param position
	 * @return void
	 * @throws
	 */
	private void setCurCircleInfo(int position)
	{
		if(datas.size()>0){
			if(position<=datas.size()){
				curData = datas.get(position - 1);
			}
			
		}
		
		curText = getCurText(position);
		curColor = getCurColor(position);
	}

	private int getCurColor(int position)
	{
		if (position > colors.length)
			return -1;
		return colors[position - 1];
	}

	private String getCurText(int position)
	{
		
		if (position > datas.size())
			return "";
		if(datas.get(position - 1) == 0){
			return "--";
		}
		
		return String.valueOf(datas.get(position - 1) + "%");
	}

	private void drawBorderCircle(Canvas canvas)
	{
		borderCircleCenterX = getWidth() / 2; // 获取圆心的x坐标
		borderCircleCenterY = getHeight() / 2; // 获取圆心的y坐标
		int radius = borderRadius; // 圆环的半径
		borderCirclePaint.setColor(Color.LTGRAY); // 设置圆环的颜色
		borderCirclePaint.setStyle(Paint.Style.STROKE); // 设置空心
		borderCirclePaint.setStrokeWidth(6); // 设置圆环的宽度
		borderCirclePaint.setAntiAlias(true); // 消除锯齿
		canvas.drawCircle(borderCircleCenterX, borderCircleCenterY, radius,
				borderCirclePaint); // 画出圆环
	}

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(Context context, float dpValue)
	{
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	/**
	 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
	 */
	public static int px2dip(Context context, float pxValue)
	{
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}
}
